package com.jilijili.config.mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @BelongsProject: jilijili-red-book-dev
 * @BelongsPackage: com.jilijili.config.mq
 * @Author: ljj
 * @CreateTime: 2022-04-12  09:39
 * @Description: TODO
 * @Version: 1.0
 */
@Configuration
public class RabbitMQConfig {

    /**
     * 根据模型编写代码：
     * 1,定义交换机
     * 2.定义队列
     * 3。创建交换机
     * 4.创建队列
     * 5。队列和交换机的绑定
     */

    public static final String EXCHANGE_MSG = "exchange_msg";
    public static final String QUEUE_SYS_MSG = "queue_sys_msg";

    //路由规则
    public static final String ROUTING_SYS_MSG = "routing.sys.msg.*";

    @Bean(EXCHANGE_MSG)
    public Exchange createExchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_MSG) // 创建交换机
                .durable(true) // 消息是否持久化
                .build(); //构建
    }

    @Bean(QUEUE_SYS_MSG)
    public Queue createQueue() {
        return new Queue(QUEUE_SYS_MSG); // 创建队列
    }

    // 队列和交换机的绑定
    @Bean
    public Binding binding(@Qualifier(EXCHANGE_MSG) Exchange exchange,
                           @Qualifier(QUEUE_SYS_MSG) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange)
                .with("routing.sys.msg.*")
                .noargs();
    }


}
